﻿using DevExpress.XtraGrid.Demos.util;
using DiaoPaiDaYin;
using Luna小店.DTO;
using Luna小店.UC.款式异常;
using Luna小店.UC.门店下单.DTO;
using mendian;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Luna小店.service
{
    /// <summary>
    /// 尺寸服务
    /// </summary>
    public class SizeService
    {
        /// <summary>
        /// 获取选择的尺码的尺寸
        /// </summary>
        /// <param name="size"></param>
        /// <param name="styleid"></param>
        /// <returns></returns>
        public static DataTable StyleValue(String size, String styleid, DataTable stylesizedt)
        {
            DataRow st = SQLmtm.GetDataRow("SELECT * FROM s_style_p WHERE SYS_STYLE_ID = '" + styleid + "'");
            DataTable dt = new DataTable();
            foreach (DataRow dr in stylesizedt.Rows)
            {
                if (dr["尺寸"].ToString() == size)
                {
                    String sql = "SELECT\n" +
                    "	*,\n" +
                    "	SUBSTRING_INDEX( SIZE_CD, '-',- 1 ) \n" +
                    "FROM\n" +
                    "	`a_size_fit_p` \n" +
                    "WHERE\n" +
                    "	FIT_CD = '" + st["STYLE_FIT_CD"].ToString() + "' \n" +
                    "	AND STYLE_CATEGORY_CD = '" + st["STYLE_CATEGORY_CD"].ToString() + "' \n" +
                    "	AND SIZEGROUP_CD = '" + st["STYLE_SIZE_GROUP_CD"] + "' \n" +
                    "	AND SIZE_CD = '" + dr["SIZE_CD"].ToString() + "';";
                    dt = SQLmtm.GetDataTable(sql);
                    break;
                }
            }
            return dt;
        }

        /// <summary>
        /// 获得STYLE_SIZE_CD
        /// </summary>
        /// <param name="size"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static String SizeCD(String size, DataTable dt)
        {
            string str = "";
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["尺寸"].ToString() == size)
                {
                    str = dr["SIZE_CD"].ToString();
                    break;
                }

            }
            return str;
        }

        /// <summary>
        /// 成衣尺寸数据
        /// </summary>
        /// <param name="styleid"></param>
        /// <returns></returns>
        public static List<成衣尺寸DTO> Get成衣尺寸DTO(String styleid)
        {
            List<成衣尺寸DTO> cycc = new List<成衣尺寸DTO>();
            String sql = "SELECT\n" +
                "	ITEM_CD AS itemCd,\n" +
                "	ITEM_VALUE AS itemValue,\n" +
                "	FIT_VALUE AS fitValue,\n" +
                "	IN_VALUE AS inValue,\n" +
                "	OUT_VALUE AS outValue \n" +
                "FROM\n" +
                "	s_style_fit_r \n" +
                "WHERE\n" +
                "	STYLE_ID = '" + styleid + "'";
            DataRow chengyidr = SQLmtm.GetDataRow(sql);
            List<String> itemCdList = new List<String>(chengyidr["itemCd"].ToString().Split(','));
            List<String> itemValueList = new List<String>(chengyidr["itemValue"].ToString().Split(','));
            List<String> fitValueList = new List<String>(chengyidr["fitValue"].ToString().Split(','));

            List<Tradd> itemCdHoldList = new List<Tradd>();
            List<Tradd> itemValueHoldList = new List<Tradd>();
            List<Tradd> fitValueHoldList = new List<Tradd>();
            int i = 0;
            foreach (String cdvl in itemCdList)
            {
                itemCdHoldList.Add(new Tradd(i, cdvl));
                i++;
            }
            i = 0;
            foreach (String cdvl in itemValueList)
            {
                itemValueHoldList.Add(new Tradd(i, cdvl));
                i++;
            }
            i = 0;
            foreach (String cdvl in fitValueList)
            {
                fitValueHoldList.Add(new Tradd(i, cdvl));
                i++;
            }
            foreach (Tradd tradd1 in itemCdHoldList)
            {
                foreach (Tradd tradd2 in itemValueHoldList)
                {
                    if (tradd1.i == tradd2.i)
                    {
                        foreach (Tradd tradd3 in fitValueHoldList)
                        {
                            if (tradd2.i == tradd3.i)
                            {
                                cycc.Add(new 成衣尺寸DTO(tradd1.str, tradd2.str, tradd3.str));
                                break;
                            }
                        }
                        break;
                    }
                }
            }
            return cycc;
        }

        public static List<尺寸呈现dto> GetThisSize(Dto定制下单 dto)
        {
            return GetThisSize(dto.STYLE_FIT_CD, dto.STYLE_CATEGORY_CD, dto.STYLE_SIZE_CD, dto.STYLE_SIZE_GROUP_CD, dto.Style_Id,true);
        }


        private static List<尺寸呈现dto> GetThisSize(String STYLE_FIT_CD,String STYLE_CATEGORY_CD,String STYLE_SIZE_CD,String STYLE_SIZE_GROUP_CD,String Style_Id,Boolean isRef)
        {
            List<尺寸呈现dto> 尺寸呈现 = new List<尺寸呈现dto>();
            String sql;
            //if (!isRef)  //  是否标准款
            //{
            //    sql = "SELECT\n" +
            //        "	S1.*,\n" +
            //        "	S2.* \n" +
            //        "FROM\n" +
            //        "	( SELECT * FROM a_size_fit_p WHERE FIT_CD = 'FIT_BODY_TYPE-1914F' AND STYLE_CATEGORY_CD = 'STYLE_CATEGORY-SHIRT' AND SIZE_CD = 'EGS_GROUP_SIZE-42' AND SIZEGROUP_CD = 'GROUP_SIZE-EGS_GROUP_SIZE' ) AS s1\n" +
            //        "	LEFT JOIN ( SELECT * FROM a_reasonable_p WHERE styleID IN ( SELECT REF_STYLE_ID FROM s_style_p WHERE SYS_STYLE_ID = " + Style_Id + " ) ) AS s2 ON s1.ITEM_VALUE = s2.itemValue;";
            //}
            //else {
                sql = "SELECT\n" +
                "	S1.*,\n" +
                "	S2.* \n" +
                "FROM\n" +
                "	( SELECT * FROM a_size_fit_p WHERE" +
                " FIT_CD = '" + STYLE_FIT_CD + "'" +
                " AND STYLE_CATEGORY_CD = '" + STYLE_CATEGORY_CD + "'" +
                " AND SIZE_CD = '" + STYLE_SIZE_CD + "'" +
                " AND SIZEGROUP_CD = '" + STYLE_SIZE_GROUP_CD + "' ) AS s1\n" +
                "	LEFT JOIN ( SELECT * FROM a_reasonable_p WHERE styleID = " + Style_Id + " ) AS s2 ON s1.ITEM_VALUE = s2.itemValue;";
            //}

            DataTable dt = SQLmtm.GetDataTable(sql);
            if(dt.Rows.Count==0)
            {
                sql = "SELECT\n" +
                    "	S1.*,\n" +
                    "	S2.* \n" +
                    "FROM\n" +
                    "	( SELECT * FROM a_size_fit_p WHERE FIT_CD = 'FIT_BODY_TYPE-1914F' AND STYLE_CATEGORY_CD = 'STYLE_CATEGORY-SHIRT' AND SIZE_CD = 'EGS_GROUP_SIZE-42' AND SIZEGROUP_CD = 'GROUP_SIZE-EGS_GROUP_SIZE' ) AS s1\n" +
                    "	LEFT JOIN ( SELECT * FROM a_reasonable_p WHERE styleID IN ( SELECT REF_STYLE_ID FROM s_style_p WHERE SYS_STYLE_ID = " + Style_Id + " ) ) AS s2 ON s1.ITEM_VALUE = s2.itemValue;";
                dt = SQLmtm.GetDataTable(sql);
            }
            foreach (DataRow dr in dt.Rows)
            {
                尺寸呈现.Add(
                    new 尺寸呈现dto(
                        dr["ITEM_CD"].ToString()
                        , dr["ITEM_VALUE"].ToString()
                        , ""
                        , ""
                        , Convert.ToDouble(dr["ITEM_FIT_VALUE"].ToString())
                        , 0
                        , 0
                        , dr["ITEM_NAME_CN"].ToString()
                        , Convert.ToDouble(dr["leastReasonable"].ToString() == "" ? "0" : dr["leastReasonable"].ToString())
                        , Convert.ToDouble(dr["maxReasonable"].ToString() == "" ? "0" : dr["maxReasonable"].ToString())
                        )
                    );
            }
            return 尺寸呈现;
        }

        public static List<尺寸呈现dto> getDto尺寸ByOrderId(String orderId, String STYLE_FIT_CD, String STYLE_CATEGORY_CD, String STYLE_SIZE_CD, String STYLE_SIZE_GROUP_CD,String CUSTOMER_ID) {
            //  尺寸
            String sql = "SELECT\n" +
                "	STYLE_ID,\n" +
                "	PHASE_CD,\n" +
                "	ITEM_CD,\n" +
                "	ITEM_VALUE,\n" +
                "	FIT_VALUE,\n" +
                "	FM_VALUE,\n" +
                "	DELETE_FLAG,\n" +
                "	VERSION,\n" +
                "	CREATE_USER,\n" +
                "	IN_VALUE,\n" +
                "	OUT_VALUE \n" +
                "FROM\n" +
                "	s_style_fit_r \n" +
                "WHERE\n" +
                "	STYLE_ID IN ( SELECT STYLE_ID FROM o_order_p WHERE ORDER_ID = '"+ orderId + "' );";
            DataTable dt = SQLmtm.GetDataTable(sql);
            //  客户量体值
            sql = "SELECT\n" +
                "	customer_fit.CUSTOMER_FIT_ID,\n" +
                "	customer_fit.CUSTOMER_ID,\n" +
                "	customer_fit.STYLE_CATEGORY_CD,\n" +
                "	customer_fit.ITEM_CD,\n" +
                "	customer_fit.ITEM_VALUE,\n" +
                "	customer_fit.FIT_VALUE,\n" +
                "	property.PROPERTY_NAME_CN,\n" +
                "	property.PROPERTY_CD,\n" +
                "	property.PROPERTY_VALUE,\n" +
                "	property.PROPERTY_UNIT_CD,\n" +
                "	customer_fit.STYLE_CATEGORY_CD \n" +
                "FROM\n" +
                "	a_customer_fit_r customer_fit\n" +
                "	LEFT JOIN a_fit_property_p property ON property.PROPERTY_CD = customer_fit.ITEM_CD \n" +
                "	AND property.PROPERTY_VALUE = customer_fit.ITEM_VALUE \n" +
                "WHERE\n" +
                "	customer_fit.DELETE_FLAG = 0 \n" +
                "	AND customer_fit.CUSTOMER_ID = '" + CUSTOMER_ID + "' \n" +
                "	AND customer_fit.CREATE_DATE = ( SELECT CREATE_DATE FROM a_customer_fit_r WHERE CUSTOMER_ID = '"+ CUSTOMER_ID + "' ORDER BY CREATE_DATE DESC LIMIT 1 ) \n" +
                "ORDER BY\n" +
                "	property.PROPERTY_COST";
            DataTable dataTable客户量体值 = SQLmtm.GetDataTable(sql);

            sql = "select Garment_size,Customer_measurement from garmentsize_to_customermeasurement;";
            DataTable dataTable对应关系 = SQLmtm.GetDataTable(sql);
            Dictionary<String, String> sizePairs = new Dictionary<string, string>();    //  量体制对应关系
            foreach (DataRow dataRow in dataTable对应关系.Rows) {
                sizePairs.Add(dataRow["Garment_size"].ToString(), dataRow["Customer_measurement"].ToString());
            }

            DtoSize Dto尺寸;
            foreach (DataRow dr in dt.Rows)
            {
                Dto尺寸 = new DtoSize(dr);
                List<尺寸呈现dto> 尺寸呈现dto = SizeService.GetThisSize(STYLE_FIT_CD, STYLE_CATEGORY_CD, STYLE_SIZE_CD, STYLE_SIZE_GROUP_CD, dr["STYLE_ID"].ToString(),true);
                Dto尺寸.build尺寸呈现dto(尺寸呈现dto, dataTable客户量体值, sizePairs);
                return 尺寸呈现dto;
            }


            throw new Exception("没有量体值");
        }
       
        public static void save设计点(String Style_Id, DtoSize Dto尺寸)
        {
            SQLmtm.DoUpdate(
                "s_style_fit_r"
                , new string[] { "PHASE_CD", "ITEM_CD", "ITEM_VALUE", "FIT_VALUE", "FM_VALUE", "DELETE_FLAG", "VERSION", "CREATE_USER", "IN_VALUE", "OUT_VALUE" }
                , new string[] { "AUDIT_PHASE_CD-PHASE_CD_10", Dto尺寸.itemCd, Dto尺寸.itemValue, Dto尺寸.fitValue, Dto尺寸.fmValue, "0", "1", "46", Dto尺寸.inValue, Dto尺寸.outValue }
                , new string[] { "STYLE_ID" }
                , new string[] { Style_Id }
                );
        }
        /// <summary>
        /// 使用orderID返回尺寸呈现dto
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public static List<尺寸呈现dto> getDto尺寸WithOrderId(String orderid)
        {
            String sql = "SELECT\n" +
                "	o_order_p.ORDER_ID,\n" +
                "	o_order_p.STYLE_ID,\n" +
                "	o_order_p.CUSTOMER_ID,\n" +
                "	s_style_p.STYLE_FIT_CD,\n" +
                "	s_style_p.STYLE_CATEGORY_CD,\n" +
                "	s_style_p.STYLE_SIZE_CD,\n" +
                "	s_style_p.STYLE_SIZE_GROUP_CD \n" +
                "FROM\n" +
                "	o_order_p\n" +
                "	LEFT JOIN s_style_p ON o_order_p.STYLE_ID = s_style_p.SYS_STYLE_ID \n" +
                "WHERE\n" +
                "	ORDER_ID = '"+orderid+"'";
            DataRow dr = SQLmtm.GetDataRow(sql);
            List<尺寸呈现dto> dtos = new List<尺寸呈现dto>();
            try
            {
                dtos = getDto尺寸ByOrderId(orderid, dr["STYLE_FIT_CD"].ToString(), dr["STYLE_CATEGORY_CD"].ToString(), dr["STYLE_SIZE_CD"].ToString(), dr["STYLE_SIZE_GROUP_CD"].ToString(), dr["CUSTOMER_ID"].ToString());
            }
            catch
            {

            }
            return dtos;
        }
        /// <summary>
        /// 获取所有尺寸
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAllSize()
        {
            String sql = "SELECT DISTINCT\n" +
            "	SUBSTRING_INDEX( SIZE_CD, '-',- 1 ) AS SIZE,\n" +
            "	SIZE_CD \n" +
            "FROM\n" +
            "	a_size_fit_p";
            return SQLmtm.GetDataTable(sql);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fit_cd"></param>
        /// <param name="size_cd"></param>
        /// <param name="styleid"></param>
        /// <returns></returns>
        public static DTOStyleMeasureItems Get尺寸Use接口(String fit_cd,String size_cd,String styleid,String categoryId)
        {
            return FunctionHelper.JsonDeserialization<DTOStyleMeasureItems>(RestCall.httpGetMethod(URL.接口url + "/style/generateStyleMeasureItems?SIZE_CD=" + size_cd + "&styleID=" + styleid+ "&categoryId="+ categoryId));
        }

        public static void test(DataTable dt)
        {
            List<dtosize> dtosizes = new List<dtosize>();
            List<dtosize> dtosizes1 = new List<dtosize>();
            foreach(DataRow dr in dt.Rows)
            {
                dr[0].ToString();//取CD和vl
                dtosizes.Add(new dtosize());
            }
            for(int i=0; i>0; i++)
            {
                dtosizes1.Add(new dtosize().build(dtosizes[i].cd, dtosizes[i].vl, dtosizes[i].fit));
            }
        }
        public class dtosize
        {
            public String cd;
            public String vl;
            public String fit;
            public String size;
            public dtosize build(String cd,String vl,String fit)
            {
                this.cd = cd;
                this.vl = vl;
                this.fit = fit;
                return this;
            }
        }

        public static List<MeasureItemDTO> 新尺寸呈现(String orderId, String STYLE_FIT_CD, String STYLE_CATEGORY_CD, String STYLE_SIZE_CD, String STYLE_SIZE_GROUP_CD, String CUSTOMER_ID)
        {
            String styleid = SQLmtm.GetDataRow("SELECT STYLE_ID FROM o_order_p WHERE ORDER_ID = '" + orderId + "'")["STYLE_ID"].ToString();
            DTOStyleMeasureItems dTOStyleMeasureItems = new DTOStyleMeasureItems();
            List<MeasureItemDTO> dTOMeasureItems = new List<MeasureItemDTO>();
            String sql = "SELECT\n" +
                "	STYLE_ID,\n" +
                "	PHASE_CD,\n" +
                "	ITEM_CD,\n" +
                "	ITEM_VALUE,\n" +
                "	FIT_VALUE,\n" +
                "	FM_VALUE,\n" +
                "	DELETE_FLAG,\n" +
                "	VERSION,\n" +
                "	CREATE_USER,\n" +
                "	IN_VALUE,\n" +
                "	OUT_VALUE \n" +
                "FROM\n" +
                "	s_style_fit_r \n" +
                "WHERE\n" +
                "	STYLE_ID IN ( SELECT STYLE_ID FROM o_order_p WHERE ORDER_ID = '" + orderId + "' );";
            DataRow dr = SQLmtm.GetDataRow(sql);
            //  客户量体值
            sql = "SELECT\n" +
                "	customer_fit.CUSTOMER_FIT_ID,\n" +
                "	customer_fit.CUSTOMER_ID,\n" +
                "	customer_fit.STYLE_CATEGORY_CD,\n" +
                "	customer_fit.ITEM_CD,\n" +
                "	customer_fit.ITEM_VALUE,\n" +
                "	customer_fit.FIT_VALUE,\n" +
                "	property.PROPERTY_NAME_CN,\n" +
                "	property.PROPERTY_CD,\n" +
                "	property.PROPERTY_VALUE,\n" +
                "	property.PROPERTY_UNIT_CD,\n" +
                "	customer_fit.STYLE_CATEGORY_CD \n" +
                "FROM\n" +
                "	a_customer_fit_r customer_fit\n" +
                "	LEFT JOIN a_fit_property_p property ON property.PROPERTY_CD = customer_fit.ITEM_CD \n" +
                "	AND property.PROPERTY_VALUE = customer_fit.ITEM_VALUE \n" +
                "WHERE\n" +
                "	customer_fit.DELETE_FLAG = 0 \n" +
                "	AND customer_fit.CUSTOMER_ID = '" + CUSTOMER_ID + "' \n" +
                "	AND customer_fit.CREATE_DATE = ( SELECT CREATE_DATE FROM a_customer_fit_r WHERE CUSTOMER_ID = '" + CUSTOMER_ID + "' ORDER BY CREATE_DATE DESC LIMIT 1 ) \n" +
                "ORDER BY\n" +
                "	property.PROPERTY_COST";
            DataTable dataTable客户量体值 = SQLmtm.GetDataTable(sql);

            dTOStyleMeasureItems = FunctionHelper.JsonDeserialization<DTOStyleMeasureItems>(RestCall.httpGetMethod(URL.接口url + "/style/generateStyleMeasureItems?SIZE_CD=" + STYLE_SIZE_CD + "&styleID=" + styleid + "&categoryId=" + STYLE_CATEGORY_CD));

            return dTOMeasureItems;

        }
    }
}
